const AdminBiz = require('../../../../biz/admin_biz.js');
const pageHelper = require('../../../../helper/page_helper.js');
const cloudHelper = require('../../../../helper/cloud_helper.js');

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
        showMeetJoin:false
    },

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
        console.info("onLoad go on");
        if (!AdminBiz.isAdmin(this)) return;
        if (pageHelper.getOptions(this, options, 'mark')){
            console.info("mark:"+ this.data.mark);
            this.setData({
                showMeetJoin:true
            })
            console.info("showMeetJoin:"+ this.data.showMeetJoin);
        }
        if (pageHelper.getOptions(this, options, 'title')){
            console.info("title:" + this.data.title);
        }
        if (pageHelper.getOptions(this, options, 'meetId')){
            console.info("meetId:" + this.data.meetId);
        }

		//设置搜索菜单
		await this._getSearchMenu();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: async function () {},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	url: async function (e) {
		pageHelper.url(e, this);
	},


	bindCommListCmpt: function (e) {
		pageHelper.commListListener(this, e);
    },
    bindNavigateToMeetList: async function (e) {
        let openId = pageHelper.dataset(e, 'id');
        console.info("bindNavigateToMeetList go on");
        console.info("openId:"+ openId);
        wx.navigateTo({
            url: '/projects/A00/meet/index/meet_index?id=1&userId=' + openId
        });
    },
    bindJoinTap: async function (e) {
        let dayIdx = pageHelper.dataset(e, 'dayidx');
        let timeIdx = pageHelper.dataset(e, 'timeidx');

        let time = this.data.meet.MEET_DAYS_SET[dayIdx].times[timeIdx];


        if (time.error) {
            if (time.error.includes('预约'))
                return pageHelper.showModal('该时段' + time.error + '，换一个时段试试吧！');
            else
                return pageHelper.showModal('该时段预约' + time.error + '，换一个时段试试吧！');
        }

        let meetId = this.data.id;
        let timeMark = time.mark;

        let callback = async () => {
            try {
                let opts = {
                    title: '请稍候',
                }
                let params = {
                    meetId,
                    timeMark
                }
                await cloudHelper.callCloudSumbit('meet/before_join', params, opts).then(res => {
                    wx.navigateTo({
                        url: `../join/meet_join?id=${meetId}&timeMark=${timeMark}`,
                    })
                });
            } catch (ex) {
                console.log(ex);
            }
        }
        MeetBiz.subscribeMessageMeet(callback);

    },
    bindMeetJoinTap: async function (e) {
        console.info("bindMeetJoinTap go on");
        let userId = pageHelper.dataset(e, 'id');
        let meetId = this.data.meetId;
        let mark = this.data.mark;

		let params = {
			userId
        }
        console.info("userId:"+userId);
        console.info("meetId:"+meetId);
        console.info("mark:"+ mark);
        wx.navigateTo({
            url: `/projects/A00/meet/join/meet_join?id=${meetId}&timeMark=${mark}&userId=${userId}`,
        })
        return;
        let forms = e.detail;

        let callback = async () => {
            try {
                let opts = {
                    title: '提交中'
                }
                let params = {
                    meetId: this.data.id,
                    timeMark: this.data.timeMark,
                    forms
                }
                await cloudHelper.callCloudSumbit('meet/join', params, opts).then(res => {
                    let content = '预约成功！'

                    let joinId = res.data.joinId;
                    wx.showModal({
                        title: '温馨提示',
                        showCancel: false,
                        content,
                        success() {
                            let ck = () => {
                                wx.reLaunch({
                                    url: pageHelper.fmtURLByPID('/pages/my/join_detail/my_join_detail?flag=home&id=' + joinId),
                                })
                            }
                            ck();
                        }
                    })
                })
            } catch (err) {
                console.log(err);
            };
        }

        // 消息订阅
        await MeetBiz.subscribeMessageMeet(callback);

    },
	bindDelTap: async function (e) {
		if (!AdminBiz.isAdmin(this)) return;
		let id = pageHelper.dataset(e, 'id');

		let params = {
			id
		}

		let callback = async () => {
			try {
				let opts = {
					title: '删除中'
				}
				await cloudHelper.callCloudSumbit('admin/user_del', params, opts).then(res => {
					
					pageHelper.delListNode(id, this.data.dataList.list, 'USER_MINI_OPENID');
					this.data.dataList.total--;
					this.setData({
						dataList: this.data.dataList
					});
					pageHelper.showSuccToast('删除成功');
				});
			} catch (e) {
				console.log(e);
			}
		}
		pageHelper.showConfirm('确认删除？删除不可恢复', callback);

	},

	bindStatusTap: async function (e) {
		if (!AdminBiz.isAdmin(this)) return;
		let id = pageHelper.dataset(e, 'id');
		let status = pageHelper.dataset(e, 'status');

		let params = {
			id,
			status
		}
		try {
			await cloudHelper.callCloudSumbit('admin/user_status', params).then(res => {
				pageHelper.modifyListNode(id, this.data.dataList.list, 'USER_STATUS', status, 'USER_MINI_OPENID');
				this.setData({
					dataList: this.data.dataList
				});
				pageHelper.showSuccToast('设置成功');
			});
		} catch (e) {
			console.log(e);
		}
	},

	_getSearchMenu: async function () {

		let sortItems = [];
		let sortMenus = [{
				label: '全部',
				type: '',
				value: ''
			}, {
				label: '正常',
				type: 'status',
				value: 1
			}, 
			{
				label: '注册时间正序',
				type: 'sort',
				value: 'newasc'
			},
			{
				label: '注册时间倒序',
				type: 'sort',
				value: 'newdesc'
			},

		]
		this.setData({
			sortItems,
			sortMenus
		})


	}

})